package org.brownflat.watergate.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Entity
@Table(name = "wgate_commit_element")
public class CommitElement extends BaseEntity {

    @Column
    private String path;

    @Enumerated(EnumType.STRING)
    @Column(name="element_action")
    private CommitElementAction commitElementAction;

    @ManyToOne
    @JoinColumn(name="commit")
    private Commit commit;

    @Column(name="before_content")
    private String beforeContent;

    @Column(name="after_content")
    private String afterContent;

    @Column(name="diff_content")
    private String diffContent;

    @Enumerated(EnumType.STRING)
    @Column(name="element_type")
    private CommitElementType commitElementType;

    public Commit getCommit() {
        return commit;
    }

    public void setCommit(Commit commit) {
        this.commit = commit;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public CommitElementAction getCommitElementAction() {
        return commitElementAction;
    }

    public boolean is(CommitElementAction action) {
        return action.equals(commitElementAction);
    }

    public boolean is(CommitElementType type) {
        return type.equals(commitElementType);
    }

    public void setCommitElementAction(CommitElementAction commitElementAction) {
        this.commitElementAction = commitElementAction;
    }

    public String getAfterContent() {
        return afterContent;
    }

    public void setAfterContent(String afterContent) {
        this.afterContent = afterContent;
    }

    public String getBeforeContent() {
        return beforeContent;
    }

    public void setBeforeContent(String beforeContent) {
        this.beforeContent = beforeContent;
    }

    public String getDiffContent() {
        return diffContent;
    }

    public void setDiffContent(String diffContent) {
        this.diffContent = diffContent;
    }

    public String getFullPath() {
        String branchPath = commit.getBranch().getRepository().getUrl();
        return String.format("%s%s", branchPath, getPath());
    }

    public void setCommitElementType(CommitElementType type) {
        this.commitElementType = type;
    }

    public CommitElementType getCommitElementType() {
        return this.commitElementType;
    }

    public boolean hasDiffContent() {
        return (beforeContent != null || afterContent != null);
    }
}
